// miniprogram/pages/ch-add_zhujiahao/ch-add_zhujiahao.js
const tableCh = wx.cloud.database().collection("ch");
const db = wx.cloud.database()
import sendRequest from "../../util/sendRequest"
Page({
  inputchName(e) {
    console.log(e);
    this.data.chname = e.detail.detail.value;
  },
  
  handleClick(e) {
    if (this.data.id) {
      sendRequest({
        url: "chUpdate",
        method: "POST",
        data: {
          id : this.data.id,
          chName : this.data.chname,
          bookId: this.data.changeBook == undefined ? this.data.bid : this.data.changeBook.id
        },
        success: res => {
          console.log("success", res)
          if (res.data.code == 200) {
            wx.showToast({
              title: res.data.msg,
              icon: 'success'
            })
            setTimeout(wx.navigateBack, 1500);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        },
        fail: res => {
          console.log("fail", res)
        },
        complete: res => {
          console.log("complete", res)
        }
      })
    } else {
      sendRequest({
        url: "chAdd",
        method: "POST",
        data: {
          chName: this.data.chname,
          bookId: this.data.changeBook == undefined ? this.data.bid : this.data.changeBook.id
        },
        success: res => {
          console.log("success", res)
          if (res.data.code == 200) {
            wx.showToast({
              title: res.data.msg,
              icon: 'success'
            })
            setTimeout(wx.navigateBack, 1500);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        },
        fail: res => {
          console.log("fail", res)
        },
        complete: res => {
          console.log("complete", res)
        }
      })
    }
  },
  
  /**
   * 页面的初始数据
   */
  data: {
    bookNameLst : [],
    index : 0,
  },
  //选择书本滚动
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      changeBook:this.data.bookLst[e.detail.value]
    })
    console.log(this.data.changeBook)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showToast({
      title: '正在查询数据',
      icon: 'loading',
      duration: 9000
    })
    console.log(options.id)
    if (options.id){
      sendRequest({
        url: "chQueryByKey",
        method: "GET",
        data: {
          id: options.id
        },
        success: res => {
          console.log("qwe", res)
          this.data.bid = res.data.data.book.id;
          let bookId = res.data.data.book.id;
          if (res.data.code == 200) {
            this.setData({
              id: res.data.data.id,
              chname: res.data.data.chname
            })
            sendRequest({
              url: "bookCheckAllZJH",
              method: "GET",
              success: res => {
                console.log("success", res)
                if (res.data.code == 200) {
                  console.log(res);
                  let book_i = 0;
                  for (var i in res.data.data) {
                    if (res.data.data[i].id == bookId) {
                      this.setData({
                        index: book_i
                      })
                    }
                    this.data.bookNameLst.push(res.data.data[i].bookname);
                    book_i++;
                  }
                  this.setData({
                    bookLst: res.data.data,
                    bookNameLst: this.data.bookNameLst
                  })
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'success'
                  })
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                }
              },
              fail: res => {
                console.log("fail", res)
              },
              complete: res => {
                console.log("complete", res)
              }
            })
            wx.showToast({
              title: res.data.msg,
              icon: 'success'
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        },
        fail: res => {
          console.log("fail", res)
        },
        complete: res => {
          console.log("complete", res)
        }
      })
    }else{
    sendRequest({
      url: "bookCheckAllZJH",
      method: "GET",
      success: res => {
        console.log("ss", res)
        if (res.data.code == 200) {
          this.data.bid = res.data.data[0].id;
           let bookId = res.data.data.id;
          let book_i = 0;
          for (var i in res.data.data) {
            if (res.data.data[i].id == bookId){
              this.setData({
                index: book_i,
                changeBook:res.data.data[i]//
              })
            }
            this.data.bookNameLst.push(res.data.data[i].bookname);
            book_i++;
          }
          this.setData({
            bookLst: res.data.data,
            bookNameLst: this.data.bookNameLst
          })
          wx.showToast({
            title: res.data.msg,
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
      fail: res => {
        console.log("fail", res)
      },
      complete: res => {
        console.log("complete", res)
      }
    })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})